const { MongoClient, ServerApiVersion } = require("mongodb");
// RssOydxK5FtfwSC2
const uri = "mongodb+srv://root:RssOydxK5FtfwSC2@cluster0.9t1f0.mongodb.net/shop?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    serverApi: ServerApiVersion.v1,
});

let _db;
exports.createMongoClient = (cb) => {
    client.connect()
    .then(client => {
        console.log('数据库已连接');
        _db = client.db();
        cb();
    })
    .catch((err) => {
        throw err;
    });
}

exports.getdb = () => {
    if(_db) {
        return _db;
    }
    throw new Error('连接未建立');
}
